import 'package:flutter/material.dart';
import 'package:flutter_deer/database/order_status.dart';
import 'package:flutter_deer/database/tables.dart';

class AbandonOrderDialog extends StatefulWidget {
  const AbandonOrderDialog({
    super.key,
    required this.order,
    required this.onConfirm,
  });

  final AppOrderBean order;
  final void Function(String inputText, int code) onConfirm;

  @override
  State<AbandonOrderDialog> createState() => _AbandonOrderDialogState();
}

class _AbandonOrderDialogState extends State<AbandonOrderDialog> {
  // 选中的弃单类型（0：回罐，1：错误数据） 2020回罐|2030数据错误
  int _selectedType = OrderOilStatus.BACK;

  // 输入的原因文本控制器
  final TextEditingController _reasonController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    // 定义尺寸常量（对应原dp值）
    const double dp15 = 15;
    const double dp5 = 5;
    const double dp10 = 10;
    const double dp13 = 13;
    const double dp40 = 40;
    const double dp44 = 44;
    const double dp150 = 150;
    const double dp360 = 360;
    const double sp16 = 16;
    const double sp18 = 18;

    // 定义颜色常量（对应原color值）
    const Color black333 = Color(0xFF333333); // black_333333
    const Color blue3377FF = Color(0xFF3377FF); // blue_3377FF
    const Color white = Color(0xFFFFFFFF);
    const Color grayF2 = Color(0xFFF2F2F2); // f2f2f2
    const Color text222 = Color(0xFF222222); // text_222222
    const Color blue13345F = Color(0xFF13345F); // 13345F（按钮背景）

    return WillPopScope(
      onWillPop: () async => false,
      child: Dialog(
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12.0)),
        elevation: 0,
        backgroundColor: Colors.transparent,
        child: LayoutBuilder(
          builder: (context, constraints) {
            final screenWidth = MediaQuery.of(context).size.width;
            final maxWidth = screenWidth * 0.3; // 设置最大宽度为屏幕宽度的70%

            return Container(
              width: dp360,
              padding: EdgeInsets.zero,
              decoration: const BoxDecoration(
                color: white,
                borderRadius: BorderRadius.all(Radius.circular(0)), // 原布局未设置圆角，保持一致
              ),
              // 卡片内容（使用Stack放置右上角关闭按钮）
              child: Stack(
                children: [
                  // 主体内容
                  Padding(
                    padding: EdgeInsets.all(dp15),
                    child: Column(
                      mainAxisSize: MainAxisSize.min, // 高度包裹内容
                      crossAxisAlignment: CrossAxisAlignment.start, // 左对齐
                      children: [
                        // 加油信息标题
                        const Text(
                          "加油信息",
                          style: TextStyle(
                            color: black333,
                            fontSize: sp16,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                        SizedBox(height: dp5), // 底部padding转换为间距

                        // 加油内容信息
                        Text(
                          '${widget.order.nzl}号枪\t\t\t\t\t\t${widget.order.mchGoodsName}\t\t\t\t\t\t${widget.order.formatMny}元', // 原布局中是动态内容，这里留空
                          style: TextStyle(
                            color: blue3377FF,
                            fontSize: sp16,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                        SizedBox(height: dp5),

                        // 弃单类型区域
                        Row(
                          children: [
                            const Text(
                              "弃单类型",
                              style: TextStyle(
                                color: black333,
                                fontSize: sp16,
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                            SizedBox(width: dp10),
                            // 回罐选项
                            Row(
                              children: [
                                Radio(
                                  value: OrderOilStatus.BACK,
                                  groupValue: _selectedType,
                                  onChanged: (value) {
                                    setState(() => _selectedType = OrderOilStatus.BACK);
                                  },
                                  activeColor: blue3377FF, // 选中颜色
                                ),
                                const Text(
                                  "回罐",
                                  style: TextStyle(
                                    color: black333,
                                    fontSize: sp16,
                                  ),
                                ),
                              ],
                            ),
                            SizedBox(width: dp10),
                            // 错误数据选项
                            Row(
                              children: [
                                Radio(
                                  value: OrderOilStatus.BACK_ERROR,
                                  groupValue: _selectedType,
                                  onChanged: (value) {
                                    setState(() => _selectedType = OrderOilStatus.BACK_ERROR);
                                  },
                                  activeColor: blue3377FF,
                                ),
                                const Text(
                                  "错误数据",
                                  style: TextStyle(
                                    color: black333,
                                    fontSize: sp16,
                                  ),
                                ),
                              ],
                            ),
                          ],
                        ),
                        SizedBox(height: dp5),

                        // 输入原因区域
                        Container(
                          height: dp150,
                          decoration: BoxDecoration(
                            color: grayF2,
                            borderRadius: BorderRadius.circular(5), // 5dp圆角
                          ),
                          child: TextField(
                            controller: _reasonController,
                            maxLength: 200,
                            textInputAction: TextInputAction.done,
                            style: const TextStyle(
                              fontSize: 13, // 原布局sp13
                              color: black333,
                            ),
                            decoration: const InputDecoration(
                              hintText: "请输入原因（非必填）",
                              hintStyle: TextStyle(
                                color: Color(0xFF999999), // 提示文字默认灰色
                                fontSize: 13,
                              ),
                              border: InputBorder.none,
                              // 去掉默认边框
                              contentPadding: EdgeInsets.all(10),
                              // 内边距
                              counterText: "", // 隐藏字数统计
                            ),
                          ),
                        ),
                        SizedBox(height: dp15), // 按钮区域顶部间距

                        // 底部按钮区域
                        Row(
                          children: [
                            // 取消按钮
                            GestureDetector(
                              onTap: () {
                                // 取消逻辑
                                Navigator.pop(context);
                              },
                              child: Container(
                                width: dp150,
                                height: dp44,
                                alignment: Alignment.center,
                                decoration: BoxDecoration(
                                  color: white,
                                  border: Border.all(color: blue13345F, width: 1), // 边框
                                  borderRadius: BorderRadius.circular(5),
                                ),
                                child: const Text(
                                  "取消",
                                  style: TextStyle(
                                    color: text222,
                                    fontSize: sp18,
                                  ),
                                ),
                              ),
                            ),
                            const Spacer(), // 中间填充
                            // 确定按钮
                            GestureDetector(
                              onTap: () {
                                final String inputText = _reasonController.text; // 假设你有一个 TextEditingController 叫 _textController
                                widget.onConfirm(inputText, _selectedType);
                                Navigator.of(context).pop();
                              },
                              child: Container(
                                width: dp150,
                                height: dp44,
                                alignment: Alignment.center,
                                decoration: BoxDecoration(
                                  color: blue13345F,
                                  borderRadius: BorderRadius.circular(5),
                                ),
                                child: const Text(
                                  '确定',
                                  style: TextStyle(
                                    color: white,
                                    fontSize: sp18,
                                  ),
                                ),
                              ),
                            ),
                          ],
                        ),
                      ],
                    ),
                  ),

                  // 右上角关闭按钮
                  const Positioned(
                    top: 0,
                    right: 0,
                    child: Padding(
                      padding: EdgeInsets.all(dp13),
                      // child: Image.asset(
                      //   "assets/images/goods/icon_dialog_close.png", // 需要替换为实际的svg路径
                      //   width: 20, // 根据实际图标大小调整
                      //   height: 20,
                      // ),
                      // child: Image.asset('assets/images/goods/icon_dialog_close.png'),
                    ),
                  ),
                ],
              ),
            );
          },
        ),
      ),
    );
  }
}
